package com.androidtestintent2.servicetest;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static MainActivity appRef = null;
    private Button bcs,bes;
    boolean k=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appRef=this;
        setContentView(R.layout.activity_main);

        bcs=(Button)findViewById(R.id.button);
        bcs.setOnClickListener(this);
        bes=(Button)findViewById(R.id.button2);
        bes.setOnClickListener(this);

    }
    public static MainActivity getAppRef(){
        return appRef;
    }
    public void bt(String data){
        setTitle(data);
    }

    public void onClick(View view) {
        if(view==bcs){
            setTitle("waiting....alarm5");
            Intent I=new Intent(MainActivity.this,AlarmSeverice.class);
            PendingIntent PI=PendingIntent.getBroadcast(MainActivity.this,0,I,0);
            Calendar calendar=Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND,3);
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),PI);
        }
        if(view==bes){
            Intent I=new Intent();
            I.setClass(MainActivity.this,AlarmSeverice.class);
            PendingIntent PI=PendingIntent.getBroadcast(MainActivity.this,0,I,0);
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(PI);
            finish();
        }
    }
}
